package com.txzt.gaea.business.device.controller;

import com.txzt.base.core.RtnInfo;
import com.txzt.gaea.business.device.domain.model.DeviceMaintain;
import com.txzt.gaea.business.device.service.IDeviceMaintainService;
import com.txzt.gaea.business.util.TranslateUtil;
import com.txzt.gaea.common.annotation.EntityCommonValue;
import com.txzt.gaea.common.base.controller.BaseController;
import com.txzt.gaea.common.enums.DeviceStatus;
import com.txzt.gaea.common.enums.IsDeleteEnum;
import com.txzt.gaea.common.enums.MaintainType;
import org.beetl.sql.core.engine.PageQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/device/maintain")
public class DeviceMaintainController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(DeviceMaintainController.class);

    @Autowired
    private IDeviceMaintainService deviceMaintainService;

    /**
     * 分页查询设备维修
     * @param pageNumber
     * @param pageSize
     * @param deviceMaintain
     * @return
     */
    @GetMapping(value = "/listRepair")
    public RtnInfo<?> listRepair(@RequestParam(defaultValue = "1") Integer pageNumber,
                               @RequestParam(defaultValue = "10") Integer pageSize, DeviceMaintain deviceMaintain) {
        logger.info(deviceMaintain.toString());
        deviceMaintain.setMaintainType(MaintainType.REPAIR.getCode());
        PageQuery<DeviceMaintain> pageList = deviceMaintainService.pageQuery(new PageQuery<>(pageNumber, pageSize, deviceMaintain));
        pageList.getList().forEach(item -> {
            //翻译部门
            item.setDepartName(TranslateUtil.getDepartmentName(item.getDepartId()));
        });
        return RtnInfo.success(pageList);
    }

    /**
     * 分页查询设备报废
     * @param pageNumber
     * @param pageSize
     * @param deviceMaintain
     * @return
     */
    @GetMapping(value = "/listScrap")
    public RtnInfo<?> listScrap(@RequestParam(defaultValue = "1") Integer pageNumber,
                               @RequestParam(defaultValue = "10") Integer pageSize, DeviceMaintain deviceMaintain) {
        logger.info(deviceMaintain.toString());
        deviceMaintain.setMaintainType(MaintainType.SCRAP.getCode());
        PageQuery<DeviceMaintain> pageList = deviceMaintainService.pageQuery(new PageQuery<>(pageNumber, pageSize, deviceMaintain));
        pageList.getList().forEach(item -> {
            //翻译部门
            item.setDepartName(TranslateUtil.getDepartmentName(item.getDepartId()));
        });
        return RtnInfo.success(pageList);
    }

    /**
     * 新增设备维修
     * @param deviceMaintain
     * @return
     */
    @PostMapping(value = "/addRepair")
    @EntityCommonValue(operation = "CREATE")
    public RtnInfo<?> addRepair(@RequestBody DeviceMaintain deviceMaintain) {
        int count = deviceMaintainService.addRepairDeviceMaintain(deviceMaintain);
        if (count == 99) {
            return RtnInfo.error(99,"维修设备数量大于库存可用数量，操作失败！");
        }
        if (count > 0) {
            return RtnInfo.SUCCESS;
        } else {
            return RtnInfo.SERVER_ERROR;
        }
    }

    /**
     * 新增设备报废
     * @param deviceMaintain
     * @return
     */
    @PostMapping(value = "/addScrap")
    @EntityCommonValue(operation = "CREATE")
    public RtnInfo<?> addScrap(@RequestBody DeviceMaintain deviceMaintain) {
        int count = deviceMaintainService.addScrapDeviceMaintain(deviceMaintain);
        if (count == 99) {
            return RtnInfo.error(99,"报废设备数量大于库存可用数量，操作失败！");
        }
        if (count > 0) {
            return RtnInfo.SUCCESS;
        } else {
            return RtnInfo.SERVER_ERROR;
        }
    }

    /**
     * 编辑
     * @param deviceMaintain
     * @return
     */
    @PutMapping(value = "/edit")
    @EntityCommonValue(operation = "UPDATE")
    public RtnInfo<?> edit(@RequestBody DeviceMaintain deviceMaintain) {
        int count = deviceMaintainService.updateById(deviceMaintain);
        if (count > 0) {
            return RtnInfo.SUCCESS;
        } else {
            return RtnInfo.SERVER_ERROR;
        }
    }

    /**
     * 维修提交
     * @param id
     * @return
     */
    @GetMapping(value = "/submitRepair")
    @EntityCommonValue(operation = "UPDATE")
    public RtnInfo<?> submitRepair(@RequestParam Long id) {
        DeviceMaintain deviceMaintain = new DeviceMaintain();
        deviceMaintain.setId(id);
        deviceMaintain.setDeviceStatus(DeviceStatus.REPAIRING.getCode());
        int count = deviceMaintainService.updateById(deviceMaintain);
        if (count > 0) {
            return RtnInfo.SUCCESS;
        } else {
            return RtnInfo.SERVER_ERROR;
        }
    }

    /**
     * 报废提交
     * @param id
     * @return
     */
    @GetMapping(value = "/submitScrap")
    @EntityCommonValue(operation = "UPDATE")
    public RtnInfo<?> scrapRepair(@RequestParam Long id) {
        DeviceMaintain deviceMaintain = new DeviceMaintain();
        deviceMaintain.setId(id);
        deviceMaintain.setDeviceStatus(DeviceStatus.SCRAPING.getCode());
        int count = deviceMaintainService.updateById(deviceMaintain);
        if (count > 0) {
            return RtnInfo.SUCCESS;
        } else {
            return RtnInfo.SERVER_ERROR;
        }
    }

    /**
     * 维修确认
     * @param id
     * @return
     */
    @GetMapping(value = "/confirmRepair")
    @EntityCommonValue(operation = "UPDATE")
    public RtnInfo<?> confirmRepair(@RequestParam Long id) {
        DeviceMaintain deviceMaintain = new DeviceMaintain();
        deviceMaintain.setId(id);
        deviceMaintain.setDeviceStatus(DeviceStatus.REPAIRED.getCode());
        deviceMaintainService.deleteDeviceMaintain(id);
        int count = deviceMaintainService.updateById(deviceMaintain);
        if (count > 0) {
            return RtnInfo.SUCCESS;
        } else {
            return RtnInfo.SERVER_ERROR;
        }
    }

    /**
     * 报废确认
     * @param id
     * @return
     */
    @GetMapping(value = "/confirmScrap")
    @EntityCommonValue(operation = "UPDATE")
    public RtnInfo<?> confirmScrap(@RequestParam Long id) {
        DeviceMaintain deviceMaintain = new DeviceMaintain();
        deviceMaintain.setId(id);
        deviceMaintain.setDeviceStatus(DeviceStatus.SCRAPED.getCode());
        int count = deviceMaintainService.updateById(deviceMaintain);
        if (count > 0) {
            return RtnInfo.SUCCESS;
        } else {
            return RtnInfo.SERVER_ERROR;
        }
    }


    /**
     * 逻辑删除
     * @param id
     * @return
     */
    @DeleteMapping(value = "/delete")
    @EntityCommonValue(operation = "UPDATE")
    public RtnInfo<?> delete(@RequestParam Long id) {
        DeviceMaintain deviceMaintain = new DeviceMaintain();
        deviceMaintain.setId(id);
        deviceMaintain.setIsDelete(IsDeleteEnum.DELETE.getCode());
        deviceMaintainService.deleteDeviceMaintain(id);
        int count = deviceMaintainService.updateById(deviceMaintain);
        if (count > 0) {
            return RtnInfo.SUCCESS;
        } else {
            return RtnInfo.SERVER_ERROR;
        }
    }


}
